package com.cpa.admin.controller;

import com.cpa.admin.config.BaseController;
import com.cpa.admin.model.ProductDo;
import com.cpa.admin.model.UserDo;
import com.cpa.admin.service.ProductService;
import com.cpa.admin.service.UserOrderService;
import com.cpa.admin.service.UserService;
import com.cpa.admin.vo.PageVo;
import com.cpa.admin.vo.ProductVo;
import com.cpa.admin.vo.UserVo;
import com.cpa.admin.vo.request.UserSettleRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/10/31
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Value("${password.salt}")
    private String passwordSalt;

    private final UserService userService;
    private final ProductService productService;
    private final UserOrderService userOrderService;

    @Autowired
    public UserController(UserService userService, ProductService productService, UserOrderService userOrderService) {
        this.userService = userService;
        this.productService = productService;
        this.userOrderService = userOrderService;
    }

    @GetMapping("list")
    public String getList() {
        return "user/list";
    }

    @ResponseBody
    @GetMapping("data")
    public PageVo<UserVo> getPageData(@RequestParam(required = false) String username,Integer page, Integer limit) {

        Page<UserVo> userPage = PageHelper.startPage(page, limit)
                .doSelectPage(() ->userService.findAllVoByView(username));


        PageVo<UserVo> pageVo = new PageVo<>();
        pageVo.setCode(0);
        pageVo.setCount(userPage.getTotal());
        pageVo.setData(userPage.getResult());

        return pageVo;
    }

    @GetMapping("add")
    public String addUserView(HttpServletRequest request) {
        List<ProductVo> voList = productService.findIdAndName();
        request.setAttribute("voList", voList);
        return "user/add";
    }


    @PostMapping("add")
    public String addUser(UserDo userDo) {
        System.out.println(userDo.toString());

        userDo.setPassword(DigestUtils.md5DigestAsHex(((passwordSalt + userDo.getPassword()).getBytes())));
        userService.insertSelective(userDo);
        return "redirect:/user/list";
    }


    @ResponseBody
    @DeleteMapping("{id}")
    public void deleteById(@PathVariable String id) {
        userService.deleteUserAndOrder(id);
    }


    @GetMapping("edit/{id}")
    public String getEditPage(@PathVariable String id, HttpServletRequest request) {

        UserDo userDo = userService.selectByPrimaryKey(id);
        request.setAttribute("userDo",userDo);

        ProductDo productDo = productService.selectByPrimaryKey(userDo.getProductId());
        request.setAttribute("productDo",productDo);
        return "user/edit";
    }


    @PostMapping("update")
    public String update(UserDo userDo) {
        userService.updateUser(userDo);
        return "redirect:/user/list";
    }


    @ResponseBody
    @PutMapping("{id}")
    public void clearAmount(@PathVariable String id) {
        UserDo oldDo = userService.selectByPrimaryKey(id);
        oldDo.setClearAmountDate(new Date());
        userService.updateByPrimaryKeySelective(oldDo);
    }



    @GetMapping("settle/{id}")
    public String getSettlePage(@PathVariable String id, HttpServletRequest request) {
        UserDo userDo = userService.selectByPrimaryKey(id);
        request.setAttribute("userDo",userDo);
        return "user/settle";
    }


    @PostMapping("settle")
    public String settle(UserSettleRequest request) {
        userOrderService.settle(request.getUserId(), request.getSettleCount(), request.getType());
        return "redirect:/user/list";
    }

}
